package com.zilla.android.zillacore.libzilla.ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.github.snowdream.android.util.Log;
import com.squareup.picasso.Picasso;
import com.zilla.android.zillacore.libzilla.listener.OnGetEditTextValuesListener;
import com.zilla.android.zillacore.libzilla.util.ReflectUtil;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by chenze on 13-12-10.
 */
public class ZillaExpandableAdapter<T, Child> extends BaseExpandableListAdapter {

    protected List<T> parentList = new ArrayList<T>();
    protected List<List<Child>> childList = new ArrayList<List<Child>>();

    protected int parentResId = 0;
    protected int childResId = 0;
    protected Class<AbsViewHolder> parentViewHolder;
    protected Class<AbsViewHolder> childViewHolder;

    protected Context context;
    protected LayoutInflater inflater;
    /**
     * CheckBox事件，传入的模型中，字段值为1代表选中状态
     */
    private CompoundButton.OnCheckedChangeListener onCheckedChangeListener;

    OnGetEditTextValuesListener onGetEditTextValuesListener;

    public ZillaExpandableAdapter(Context context, List<T> parent, int parentResId, Class parentViewHolder,
                                  List<List<Child>> child, int childResId, Class childViewHolder) {

        this.parentList = parent;
        this.childList = child;

        this.parentResId = parentResId;
        this.childResId = childResId;

        this.parentViewHolder = parentViewHolder;
        this.childViewHolder = childViewHolder;

        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public OnGetEditTextValuesListener getOnGetEditTextValuesListener() {
        return onGetEditTextValuesListener;
    }

    public void setOnGetEditTextValuesListener(OnGetEditTextValuesListener onGetEditTextValuesListener) {
        this.onGetEditTextValuesListener = onGetEditTextValuesListener;
    }
    public CompoundButton.OnCheckedChangeListener getOnCheckedChangeListener() {
        return onCheckedChangeListener;
    }

    /**
     * 设置checkbox监听事件，传入的模型中，字段值为1代表选中状态。Model可以通过view.getTag()获得
     *
     * @param onCheckedChangeListener
     */
    public void setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener onCheckedChangeListener) {
        this.onCheckedChangeListener = onCheckedChangeListener;
    }

    @Override
    public int getGroupCount() {
        return parentList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childList.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return parentList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childList.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        return new AdapterUtil<T>(context).getView(
                parentList, inflater,
                parentResId, parentViewHolder, onCheckedChangeListener,
                groupPosition, convertView, parent);
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        List<Child> children = childList.get(groupPosition);
        return new AdapterUtil<Child>(context).getView(
                children, inflater,
                childResId, childViewHolder, onCheckedChangeListener,
                childPosition, convertView, parent);
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

}
